﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using GrayParrot.Web.Controls;
using GrayParrot.Core.Filters;
using FimetContoDeposito.DAL;
using FimetContoDeposito.BLL.Objects;

namespace FimetContoDeposito.BLL.ResultSets
{
    public class CustomerOperatorsBagResultSet : ResultSet<CustomerOperatorBag>
	{
        public CustomerOperatorsBagResultSet(DTParameters param, IQueryable<CustomerOperatorBag> result)
            : base(param, result)
        {
        }

        public override Expression<Func<CustomerOperatorBag, bool>> ApplyGeneralFilter(string searchPattern)
        {
            var predicate = PredicateBuilder.False<CustomerOperatorBag>();

            if (!string.IsNullOrWhiteSpace(searchPattern))
            {
                predicate = predicate.Or(im => im.Nome != null && im.Nome.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.Cognome != null && im.Cognome.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.Azienda != null && im.Azienda.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.Email != null && im.Email.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.Note != null && im.Note.ToString().Contains(searchPattern));
            }

            return predicate;
        }

        public override Expression<Func<CustomerOperatorBag, bool>> ApplyGeneralColumnsFilter(List<string> columnSearch)
        {
            var predicate = PredicateBuilder.True<CustomerOperatorBag>();

            string m_id = columnSearch[ColumnIndex];
            string m_nome = columnSearch[ColumnIndex];
            string m_cognome = columnSearch[ColumnIndex];
            string m_username = columnSearch[ColumnIndex];
            string m_email = columnSearch[ColumnIndex];
            string m_azienda = columnSearch[ColumnIndex];
            bool m_isApprovato = GrayParrot.Extensions.Types.TypeExtensions.GetValueOrDefault<bool>(columnSearch[ColumnIndex]);
            string m_note = columnSearch[ColumnIndex];

            if (!string.IsNullOrWhiteSpace(m_nome))
            {
                predicate = predicate.And(im => im.Nome.Equals(m_nome, StringComparison.InvariantCultureIgnoreCase));
            }

            if (!string.IsNullOrWhiteSpace(m_cognome))
            {
                predicate = predicate.And(im => im.Cognome.Equals(m_cognome, StringComparison.InvariantCultureIgnoreCase));
            }

            if (!string.IsNullOrWhiteSpace(m_username))
            {
                predicate = predicate.And(im => im.Username.Equals(m_username, StringComparison.InvariantCultureIgnoreCase));
            }

            if (!string.IsNullOrWhiteSpace(m_email))
            {
                predicate = predicate.And(im => im.Email.Equals(m_email, StringComparison.InvariantCultureIgnoreCase));
            }

            if (!string.IsNullOrWhiteSpace(m_azienda))
            {
                predicate = predicate.And(im => im.Azienda.Equals(m_azienda, StringComparison.InvariantCultureIgnoreCase));
            }

            if (!string.IsNullOrWhiteSpace(m_note))
            {
                predicate = predicate.And(im => im.Note.Equals(m_note, StringComparison.InvariantCultureIgnoreCase));
            }

            //predicate = predicate.And(im => im.IsApprovato == m_isApprovato);

            return predicate;
        }
	}
}
